import axios from 'axios';
import { useAuthStore } from '@/stores/auth';
import { ElLoading, ElMessage } from 'element-plus';

// 创建 axios 实例
const instance = axios.create({
    baseURL: '/api', // 接口统一域名
    timeout: 6000, // 设置超时
    headers: {
        'Content-Type': 'application/json;charset=UTF-8;',
    },
});

let loading; // 全局 Loading 实例
let requestCount = 0; // 正在请求的数量

// 显示 Loading
const showLoading = () => {
    if (requestCount === 0 && !loading) {
        loading = ElLoading.service({
            text: 'Loading...',
            background: 'rgba(0, 0, 0, 0.7)',
            spinner: 'el-icon-loading',
        });
    }
    requestCount++;
};

// 隐藏 Loading
const hideLoading = () => {
    requestCount--;
    if (requestCount === 0 && loading) {
        loading.close();
        loading = null;
    }
};

// 请求拦截器
instance.interceptors.request.use(
    (config) => {
        showLoading();
        const authStore = useAuthStore();
        const auth = authStore.getAuth(); // 检查是否有 Auth
        if (auth && auth.token) {
            config.headers.Authorization = `Bearer ${auth.token}`;
        }
        if (config.method === 'POST') {
            config.data = JSON.stringify(config.data);
        }
        return config;
    },
    (error) => {
        hideLoading();
        return Promise.reject(error);
    }
);

// 响应拦截器
instance.interceptors.response.use(
    (response) => {
        hideLoading();
        return response.data;
    },
    (error) => {
        hideLoading();
        if (error.response && error.response.status) {
            const status = error.response.status;
            let message = '';
            switch (status) {
                case 400:
                    message = '请求错误';
                    break;
                case 401:
                    message = '未经授权，请重新登录';
                    break;
                case 404:
                    message = '请求地址出错';
                    break;
                case 408:
                    message = '请求超时';
                    break;
                case 500:
                    message = '服务器内部错误!';
                    break;
                case 501:
                    message = '服务未实现!';
                    break;
                case 502:
                    message = '网关错误!';
                    break;
                case 503:
                    message = '服务不可用!';
                    break;
                case 504:
                    message = '网关超时!';
                    break;
                case 505:
                    message = 'HTTP版本不受支持';
                    break;
                default:
                    message = '请求失败';
            }
            ElMessage.error(message);
            return Promise.reject(error);
        }
        return Promise.reject(error);
    }
);

export default instance;
